package cn.seaboot.admin.excel.web;

import cn.seaboot.admin.excel.bean.ExcelSource;
import cn.seaboot.admin.excel.manager.ExcelManager;
import cn.seaboot.admin.excel.service.ExcelSourceService;
import cn.seaboot.admin.mvc.Pagination;
import cn.seaboot.admin.mvc.argument.Params;
import cn.seaboot.admin.user.context.UserContext;
import cn.seaboot.commons.file.ResponseWriter;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * Excel [t_sys_excel_source] - Controller
 *
 * @author ChenSS on 2018/6/27.
 * @version 1.0.0
 */
@Controller
@Tag(name = "Excel [t_sys_excel] 管理")
@RequestMapping("sys/excel/source")
public class ExcelSourceCtrl {

    @Resource
    private UserContext userContext;

    @Resource
    private ExcelManager excelManager;

    @Resource
    private ExcelSourceService excelSourceService;

    /**
     * excel导入，仅保存文件，并未直接生效
     *
     * @param file -
     * @return pojo
     * @throws IOException -
     */
    @ResponseBody
    @Operation(description = "Excel [t_sys_excel] 导入Excel")
    @Parameters({@Parameter(name = "type", description = "模版类型")})
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public ExcelSource importExcel(@RequestParam MultipartFile file
            , @RequestParam(defaultValue = "0") Integer type) throws IOException {
        return excelSourceService.importExcel(type, file);
    }

    /**
     * 下载Excel文件
     *
     * @param id 文件名
     * @throws IOException -
     */
    @Operation(description = "Excel [t_sys_excel] 获取Excel内容")
    @RequestMapping(value = "download/{id}", method = RequestMethod.GET)
    public void exportExcel(@PathVariable Long id
            , HttpServletRequest request, HttpServletResponse response) throws IOException {
        excelManager.downloadBook(response, id);
    }

    /**
     * 下载Excel文件
     *
     * @param id 文件名
     * @throws IOException -
     */
    @Operation(description = "Excel [t_sys_excel] 获取Excel内容")
    @RequestMapping(value = "preview/{id}", method = RequestMethod.GET)
    public void previewExcel(@PathVariable Long id, HttpServletResponse response) throws IOException {
        excelManager.previewBook(response, id);
    }

//    /**
//     * 获取文件预览数据
//     *
//     * @param id 文件名
//     * @return list
//     * @throws IOException -
//     */
//    @ResponseBody
//    @Operation(description = "Excel [t_sys_excel] 获取Excel内容")
//    @Parameters({
//            @Parameter(name = "id", description = "ID"),
//            @Parameter(name = "page", description = "页号"),
//            @Parameter(name = "limit", description = "行数")})
//    @RequestMapping(value = "content", method = RequestMethod.GET)
//    public ExcelContent readListMap(@Params Long id, @RequestParam Map<String, Object> params,
//                                    @RequestParam(defaultValue = "0") Integer start,
//                                    @RequestParam(defaultValue = "1") Integer page,
//                                    @RequestParam(defaultValue = "10") Integer limit) throws IOException {
//        try {
//            PageUtils.startPage(page, limit);
//            return excelSourceService.readExcelContent(id, start);
//        } finally {
//            PageUtils.clearPage();
//        }
//    }
//    /**
//     * 获取文件预览数据
//     *
//     * @param id 文件名
//     * @return list
//     * @throws IOException -
//     */
//    @ResponseBody
//    @Operation(description = "Excel [t_sys_excel] 获取Excel内容")
//    @Parameters({
//            @Parameter(name = "id", description = "ID"),
//            @Parameter(name = "c", description = "页号"),
//            @Parameter(name = "limit", description = "行数")})
//    @RequestMapping(value = "content/array", method = RequestMethod.GET)
//    public Object readListArray(@Params Long id, @RequestParam Map<String, Object> params) throws IOException {
//        try (InputStream is = excelSourceService.openInputStreamById(id)) {
//            PageUtils.startPage(params);
//            return PageUtils.of(excelSourceService.readListArray(is));
//        } finally {
//            PageUtils.clearPage();
//        }
//    }

//    /**
//     * 获取文件预览数据
//     *
//     * @param id 文件名
//     * @return list
//     * @throws IOException -
//     */
//    @ResponseBody
//    @Operation(description = "Excel [t_sys_excel] 获取Excel内容")
//    @Parameters({
//            @Parameter(name = "id", description = "ID"),
//            @Parameter(name = "page", description = "页号"),
//            @Parameter(name = "limit", description = "行数")})
//    @RequestMapping(value = "content/fully", method = RequestMethod.GET)
//    public List<Object[]> getExcelContent(@Params Long id, @RequestParam Map<String, Object> params) throws IOException {
//        try (InputStream is = excelSourceService.openInputStreamById(id)) {
//            return excelSourceService.readListArray(is);
//        }
//    }

    /**
     * 删除
     *
     * @param id id
     * @return pojo
     */
    @ResponseBody
    @Operation(description = "Excel [t_sys_excel] 删")
    @Parameters({@Parameter(name = "id", description = "ID")})
    @RequestMapping(value = "info", method = RequestMethod.DELETE)
    public int deleteInfo(@Params Long id) throws IOException {
        return excelSourceService.deleteById(id);
    }

    /**
     * /**
     * 查询列表
     *
     * @return PageInfo
     */
    @ResponseBody
    @Operation(description = "Excel [t_sys_excel] 列表查询")
    @Parameters({
            @Parameter(name = "page", description = "页号"),
            @Parameter(name = "limit", description = "行数")})
    @RequestMapping(value = "page", method = RequestMethod.GET)
    public PageInfo<ExcelSource> queryPage(@RequestParam Map<String, Object> params) {
        String userId = userContext.getUserId();
        return Pagination.get(params, () -> excelSourceService.queryList(userId));
    }
}
